import { createRouter, createWebHashHistory, useRoute } from 'vue-router'
import Index from '@/views/index/index.vue'
import Login from '@/views/Login/index.vue'
import Test from '@/views/Test/index.vue'
const routes = [
  {
    path: '/',
    component: Index
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/test',
    component: Test
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // 加上标题
  if (to.meta.title) document.title = '知否-' + to.meta
  // 进入白名单页面
  const whileList = ['/login']
  if (whileList.includes(to.path)) return next()
  // 去往白名单外的页面
  const token = localStorage.getItem('token')
  if (!token) return next('/login')
  next()
})
export default router
